// [1046]-最后一块石头的重量
struct Solution;

//leetcode submit region begin(Prohibit modification and deletion)
impl Solution {
    pub fn last_stone_weight(stones: Vec<i32>) -> i32 {
        let mut priority_queue = std::collections::BinaryHeap::from(stones);
        let mut ans = 0;
        while let Some(num) = priority_queue.pop() {
            ans = (ans - num).abs();
            match priority_queue.peek() {
                Some(&peek) if ans < peek => {
                    priority_queue.push(ans);
                    ans = 0;
                }
                _ => {}
            };
        }
        ans
    }
}
//leetcode submit region end(Prohibit modification and deletion)

#[test]
fn main() {
    let stones = vec![2, 7, 4, 1, 8, 1];
    assert_eq!(Solution::last_stone_weight(stones), 1);
    let stones = vec![7, 6, 7, 6, 9];
    assert_eq!(Solution::last_stone_weight(stones), 3);
}
